<?php
define('DISABLEDEFENSE', true);
define('DISABLEXSSCHECK', true);

require '../../../../class/class_core.php';

C::app()->init();
global $_G;
try{
    $result = C::m('#boan_payment#payment')->verify('alipay',$_GET);
}catch (Exception $e){
    C::m('#boan_payment#payment')->log($_GET['out_trade_no'],2,'alipay',var_export($e,true), 1);
    $result = false;
}

try{
    if($result) {
        $out_trade_no = $_GET['out_trade_no'];
        
        $trade_no = $_GET['trade_no'];
        $trade_status = $_GET['trade_status'];
        
        $order = C::m('#boan_payment#payment')->getOrder($out_trade_no);
        if(empty($order)){
            throw new Exception(lang(P_PAYMENT, 'order_err4'));
        }
        if($order['amount'] != $_GET['total_amount']){
            throw new Exception(lang(P_PAYMENT, 'order_err7'));
        }
       
        $t = strtotime($_GET['gmt_payment']);
        
        if(strpos($_G['siteurl'], 'source') !== FALSE){
            $_G['siteurl'] = substr($_G['siteurl'],0,strpos($_G['siteurl'], 'source'));
        }
        
        if (C::m('#boan_payment#payment')->checkPay($out_trade_no,'alipay')) {
           if($order['return_url']){
               dheader('location:'.$_G['siteurl'].$order['return_url']);
           }else{
               dheader('location:'.$_G['siteurl'].'home.php?mod=spacecp&ac=plugin&id=boan_payment:user_order');
           }
        }
    }else {
        throw new Exception('Validation failed',204);
    }
}catch(Exception $e){
    showmessage(lang(P_PAYMENT, 'order_err8').'<br>{err}','',array('err' => var_export($e,true)),array('alert' => 'error'));
}


